<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Links extends MX_Controller {

     var $_table = 'links';

     function __construct() {
		parent::__construct ();
		$this->load->model( 'links_model' );
		$this->load->model ( '../../gallery/models/gallery_model' );
	}

	function listlinks() {
	$data ['list'] = $this->links_model->getlinks();
	$data ['content'] = 'admin/list';
	$this->load->view ( 'back_end/index', $data );
	}

	function form_add_link(){
	$data ['content'] = 'admin/form_add_link';
	$this->load->view ( 'back_end/index', $data );
    }

	function do_add_link(){
	 if ($this->input->post ()) {
	 		if (isset($_FILES['img']) && $_FILES['img']['name']!=''){
				$data_img = array ();
				$image_data = $this->gallery_model->do_upload ();

				$data_img ['file_name'] = ( string ) $image_data ['file_name'];
				$data_img ['type'] = 'links';
				$data_img ['path'] = './storage/images';
				$img_id=$this->gallery_model->saveItem ( 'gallery', array ('field' => 'id', 'id' => 0 ), $data_img );

			}else{
				$img_id=0;
			}
			$data = array ();
			$data ['ten_link'] = ( string ) $this->input->post ( 'ten_link' );
			$data ['link'] = ( string ) $this->input->post ( 'link' );
			$data ['type'] = ( string ) $this->input->post ( 'type' );
			$data ['img_id'] = ( int ) $img_id;
			$this->links_model->saveItem ( 'links', array ('field' => 'id', 'id' => 0 ), $data );
			redirect ( 'admin/links/listlinks' );
		}
    }

	function form_sua_link() {
		$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$data['dulieu'] = $this->links_model->lay_dulieu_link_sua($id);
		$data ['content'] = 'admin/form_sua_link';
		$this->load->view ( 'back_end/index', $data );
	}

	function do_sua_link() {

	  if ($this->input->post ()) {
	  		if (isset($_FILES['img']) && $_FILES['img']['name']!=''){
				$data_img = array ();
				$image_data = $this->gallery_model->do_upload ();

				$data_img ['file_name'] = ( string ) $image_data ['file_name'];
				$data_img ['type'] = 'links';
				$data_img ['path'] = './storage/images';
				$img_id=$this->gallery_model->saveItem ( 'gallery', array ('field' => 'id', 'id' => 0 ), $data_img );

			}else{
				$img_id=0;
			}

			$id = $this->input->post ( 'id_de_sua' );
			$data ['ten_link'] = ( string ) $this->input->post ( 'ten_link' );
			$data ['link'] = ( string ) $this->input->post ( 'link' );
			$data ['type'] = ( string ) $this->input->post ( 'type' );
			if($img_id != 0){
				$data ['img_id'] = ( int ) $img_id;
			}
			$this->links_model->saveItem ( 'links', array ('field' => 'id', 'id' => $id ), $data );
			// array ('field' => 'id', 'id' => $id) tuong duong voi where `id` = $id
			redirect ( 'admin/links/listlinks' );
		}
	}

	function xoalink() {
     	$id = ($this->uri->segment ( 4 ) == '') ? 0 : $this->uri->segment ( 4 );
		$this->links_model->del ( $id );
		redirect ( 'admin/links/listlinks' );
	}
}
